package com.petcommunity.servlet;

import com.petcommunity.dao.*;
import com.petcommunity.model.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.sql.Date;

@WebServlet("/editSubmit")
public class EditSubmitServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        request.setCharacterEncoding("UTF-8");
        String type = request.getParameter("type");

        try {
            switch (type) {
                case "pet":
                    int petId = Integer.parseInt(request.getParameter("id"));
                    String name = request.getParameter("name");
                    String species = request.getParameter("species");
                    String breed = request.getParameter("breed");
                    String gender = request.getParameter("gender");
                    Date birthDate = Date.valueOf(request.getParameter("birthDate"));
                    int ownerId = Integer.parseInt(request.getParameter("ownerId"));
                    Pet pet = new Pet();
                    pet.setPetId(petId);
                    pet.setName(name);
                    pet.setSpecies(species);
                    pet.setBreed(breed);
                    pet.setGender(gender);
                    pet.setBirthDate(birthDate);
                    pet.setOwnerId(ownerId);
                    new PetDAO().updatePet(pet);
                    response.sendRedirect("pets");
                    break;

                case "owner":
                    int ownerID = Integer.parseInt(request.getParameter("id"));
                    String ownerName = request.getParameter("name");
                    String phone = request.getParameter("phone");
                    String address = request.getParameter("address");
                    Owner owner = new Owner();
                    owner.setOwnerId(ownerID);
                    owner.setName(ownerName);
                    owner.setPhone(phone);
                    owner.setAddress(address);
                    new OwnerDAO().updateOwner(owner);
                    response.sendRedirect("owners");
                    break;

                case "adoption":
                    int adoptionId = Integer.parseInt(request.getParameter("id"));
                    int petIdA = Integer.parseInt(request.getParameter("petId"));
                    String adoptername = request.getParameter("adopterName");
                    String adopterphone = request.getParameter("adopterPhone");
                    Date adoptionDate = Date.valueOf(request.getParameter("adoptionDate"));
                    Adoption adoption = new Adoption();
                    adoption.setAdoptionId(adoptionId);
                    adoption.setPetId(petIdA);
                    adoption.setAdopterName(adoptername);
                    adoption.setAdopterPhone(adopterphone);
                    adoption.setAdoptionDate(adoptionDate);
                    new AdoptionDAO().updateAdoption(adoption);
                    response.sendRedirect("adoptions");
                    break;

                case "lost":
                    int lostId = Integer.parseInt(request.getParameter("id"));
                    int lostPetId = Integer.parseInt(request.getParameter("petId"));
                    Date lostDate = Date.valueOf(request.getParameter("lostDate"));
                    String location = request.getParameter("location");
                    String description = request.getParameter("description");
                    boolean found = Boolean.parseBoolean(request.getParameter("found"));
                    LostPet lostPet = new LostPet();
                    lostPet.setLostId(lostId);
                    lostPet.setPetId(lostPetId);
                    lostPet.setLostDate(lostDate);
                    lostPet.setLocation(location);
                    lostPet.setDescription(description);
                    lostPet.setFound(found);
                    new LostPetDAO().updateLostPet(lostPet);
                    response.sendRedirect("lostpets");
                    break;

                case "vaccine":
                    int vaccineId = Integer.parseInt(request.getParameter("id"));
                    int vaccinePetId = Integer.parseInt(request.getParameter("petId"));
                    String vaccineName = request.getParameter("vaccineName");
                    Date vaccinationDate = Date.valueOf(request.getParameter("vaccinationDate"));
                    Date nextDueDate = Date.valueOf(request.getParameter("nextDueDate"));
                    VaccineRecord vaccine = new VaccineRecord();
                    vaccine.setRecordId(vaccineId);
                    vaccine.setPetId(vaccinePetId);
                    vaccine.setVaccineName(vaccineName);
                    vaccine.setVaccinationDate(vaccinationDate);
                    vaccine.setNextDueDate(nextDueDate);
                    new VaccineRecordDAO().updateVaccine(vaccine);
                    response.sendRedirect("vaccines");
                    break;

                case "notice":
                    int noticeId = Integer.parseInt(request.getParameter("id"));
                    String title = request.getParameter("title");
                    String content = request.getParameter("content");
                    Date publishDate = Date.valueOf(request.getParameter("publishDate"));
                    Notice notice = new Notice();
                    notice.setNoticeId(noticeId);
                    notice.setTitle(title);
                    notice.setContent(content);
                    notice.setCreatedAt(publishDate);
                    new NoticeDAO().updateNotice(notice);
                    response.sendRedirect("notices");
                    break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("error.jsp");
        }
    }
}

